package com.darker.demo;

import java.util.concurrent.TimeUnit;

/**
 * @auther lj
 * @date 2018/7/31 11:07
 * @email lmwtuedu@163.com
 */
public class Interrupted {

    public static void main(String[] args) throws InterruptedException {
        Thread sleepThread = new Thread(new SleepRunner(), "sleep runner");
        sleepThread.setDaemon(true);

        Thread busyThread = new Thread(new BusyRunner(), "busy runner");
        busyThread.setDaemon(true);

        sleepThread.start();
        busyThread.start();

        // sleep
        TimeUnit.SECONDS.sleep(5);

        sleepThread.interrupt();
        busyThread.interrupt();
        System.out.println("SleepThread interrupted is " + sleepThread.isInterrupted());
        System.out.println("BusyThread interrupted is " + busyThread.isInterrupted());
        // 防止sleepThread和busyThread立刻退出
        TimeUnit.SECONDS.sleep(5);

    }


    static class SleepRunner implements Runnable{
        @Override
        public void run() {
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    static class BusyRunner implements Runnable{
        @Override
        public void run() {
            while(true){
               // System.out.println("busy....");
            }
        }
    }
}